﻿
/*
 * PsTFS is a set of PowerShell commands to manage and use Team Foundation Server. PowerShell 
 * effectiveness is combined with the power of TFS API to maximizes TFS capabilities.
 * 
 * Creator : Choulant Noham
 * Mail : choulant@gmail.com
 * Blog : http://choulant.blogspot.com
 * 
 */


using System;
using System.Management.Automation;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
using Microsoft.TeamFoundation.VersionControl.Common;
using PsTFS.Objects;
using System.Collections.Generic;
using System.Linq;


namespace PsTFS.Commandes.Remove
{
    /// <summary>
    /// Ajout d'un ou plusieurs fichiers
    /// </summary>
    [Cmdlet(VerbsCommon.Remove, "Files")]
    public class Remove_Files : PsCmdlet
    {
        /// <summary>
        /// Constructeur et initialisation des données
        /// </summary>
        public Remove_Files()
        {
            this.Question = true;
        }

        /// <summary>
        /// Lancement du processus
        /// </summary>
        protected override void ProcessRecord()
        {
            try
            {
                // Récupération du Version Control. 
                VersionControlServer versionControl = Methode_Service.VCS();

                Item[] items = versionControl.GetItems(Source, VersionSpec.Latest, RecursionType.OneLevel).Items;

                string reponse = "y";
                string reponseDeleteFolder = "y";
                if (Question)
                {
                    Console.WriteLine("Are you sure to delete the folder or file ?");
                    reponse = Console.ReadLine();
                    Log.AddLog(null, "Are you sure to delete the folder or file ? : Reponse " + reponse);
                }
                if (Question)
                {
                    Console.WriteLine("Do you want to delete the folder too ?");
                    reponseDeleteFolder = Console.ReadLine();
                    Log.AddLog(null, "Do you want to delete the folder too  ? : Reponse " + reponseDeleteFolder);
                }

                if (reponse.Equals("y", StringComparison.OrdinalIgnoreCase))
                {
                    List<Item> l = new List<Item>(items);

                    foreach (Item item in l)
                    {
                        if (item.ServerItem.ToLower() != Source.ToLower() || reponseDeleteFolder.Equals("y", StringComparison.OrdinalIgnoreCase))
                        {
                            ItemSpec i = new ItemSpec(item.ServerItem, RecursionType.Full);
                            try
                            {
                                Item[] itemsDestroy = versionControl.Destroy(i, VersionSpec.Latest, null, DestroyFlags);

                                if (Log.bLog)
                                {
                                    foreach (Item im in itemsDestroy)
                                    {
                                        Log.AddLog(null, "Delete : " + im.ServerItem);
                                    }
                                }
                            }
                            catch (Microsoft.TeamFoundation.VersionControl.Client.ItemNotFoundException ex) { System.Diagnostics.Debug.WriteLine(ex); }
                        }
                    }
                    WriteObject(true);
                }
            }
            catch (Exception ex)
            {
                ErrorRecord err = new ErrorRecord(ex, Error.GetInstance().GetError(this), ErrorCategory.WriteError, null);
                WriteError(err);
                Log.AddLog(ex.StackTrace);
                WriteObject(false);
            }
            finally
            {
                base.ProcessRecord();
            }

        }

        /// <summary>
        /// Permet d'informer la fin de la commande
        /// </summary>
        protected override void EndProcessing()
        {
            Log.AddLog(null, "Remove-Files is end");
            base.EndProcessing();
        }

        /// <summary>
        /// Object contenant Team Foundation Server
        /// </summary>
        [Parameter(Position = 0)]
        public TfsTeamProjectCollection TFS
        {
            set { Context.Instance.Tfs = value; }
        }

        /// <summary>
        /// Source à supprimer
        /// </summary>
        [Parameter(Position = 1, Mandatory = true)]
        [ValidateNotNullOrEmpty]
        public string Source { get; set; }

        /// <summary>
        /// Type de suppréssion
        /// </summary>
        [Parameter(Position = 2, Mandatory = true)]
        [ValidateNotNullOrEmpty]
        public DestroyFlags DestroyFlags { get; set; }

        /// <summary>
        /// Posé ou non la question de confirmation
        /// </summary>
        [Parameter(Position = 3)]
        [ValidateNotNullOrEmpty]
        public bool Question { get; set; }
    }
}


